
package com.ys.chatserver.http.file.d;

import com.eva.framework.httpfile.FileDownload;
import com.eva.framework.utils.LoggerFactory;
import com.ys.chatserver.BaseConf;
import com.ys.chatserver.tool.EmptyUtils;
import org.slf4j.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class HumanFaceDownloader extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {

    Logger logger = LoggerFactory.getLog();

    public HumanFaceDownloader() {
        super();
    }

    protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        String file_name = req.getParameter("file_name");
        if (EmptyUtils.isNotEmpty(file_name)) {
            try {
                processBinaryDownload(res, BaseConf.getInstance().getDIR_HUMAN_FACE_UPLOAD(), file_name);
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
        }
    }

    private void processBinaryDownload(HttpServletResponse res, String file_dir, String file_name) throws Exception {
        String contentType = file_name.endsWith(".png") ? FileDownload.CONTENT_TYPE_PND : FileDownload.CONTENT_TYPE_JPEG;
        FileDownload.download(res, file_dir + file_name, file_name, contentType, false);
    }

}
